package com.yoyosys.shared.service.impl;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yoyosys.common.vo.Message;
import com.yoyosys.shared.mapper.ExchangeTaskMapper;
import com.yoyosys.shared.vo.Dependency;
import com.yoyosys.shared.po.ExchangeTask;
import com.yoyosys.shared.service.ExchangeTaskService;
import com.yoyosys.shared.vo.ExchangeTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ExchangeTaskServiceImpl implements ExchangeTaskService {
    @Autowired
    private ExchangeTaskMapper exchangeTaskMapper;
    @Override
    public Message jobinfo(ExchangeTaskVo exchangeTask) {
        Message message = new Message();
     try {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String data = sdf.format(new Date());
         ExchangeTask exchange = new ExchangeTask();
         exchange.setId(exchangeTask.getId());
         exchange.setUnitCode(exchangeTask.getUnitCode());
         exchange.setLabel(exchangeTask.getLabel());
         exchange.setDescr(exchangeTask.getDescr());
         exchange.setBatchType(exchangeTask.getBatchType());
         exchange.setCreateUser(exchangeTask.getCreateUser());
         exchange.setCreateDate(data);
         exchange.setTeamName(exchangeTask.getTeamName());
         exchange.setTeamLabel(exchangeTask.getTeamLabel());
         exchange.setLastEditor(exchangeTask.getLastEditor());
         exchange.setLastUpdateDate(data);
         exchange.setRequestId(exchangeTask.getRequestId());
         // 处理依赖关系
         if (exchangeTask.getDependencys() != null) {
             List<Dependency> dependencies = exchangeTask.getDependencys().stream()
                     .map(dto -> {
                         Dependency dependency = new Dependency();
                         dependency.setFroms(dto.getFroms());
                         dependency.setJobId(dto.getJobId());
                         dependency.setOptionDesc(dto.getOptionDesc());
                         dependency.setTo(dto.getTo());
                         dependency.setToSystem(dto.getToSystem());
                         return dependency;
                     })
                     .collect(Collectors.toList());
             ObjectMapper mapper = new ObjectMapper();
             String json = mapper.writeValueAsString(dependencies);
             exchange.setDependencys(json);
         }
         ExchangeTask exchangetask=exchangeTaskMapper.selectJobInfo(exchangeTask.getId());
         if(exchangetask==null){
             exchangeTaskMapper.jobInfo(exchange);
         }else {
             exchangeTaskMapper.updateJobInfo(exchange);
         }
         message.setSuccessMsg("任务提交成功");
         message.setData(exchange.getRequestId());
         message.setCode("200");
     }catch (Exception e){
         message.setErrorMsg("任务提交失败");
         message.setCode("500");
         message.setData(e.getMessage());
     }
        return message;
    }

}
